//
//  STFBaseView.swift
//  高仿新浪微博
//
//  Created by 沈腾飞 on 2017/1/17.
//  Copyright © 2017年 shen. All rights reserved.
//

import UIKit

class STFBaseView: UIView {

    
    //声明一个闭包
    
    var block : (()->())?

    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
  
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     func setupUI () {
        
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        //添加控件
        addSubview(feedIV)
        addSubview(iconIV)
        addSubview(keepOutIV)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加控件
        feedIV .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconIV.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        keepOutIV.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(keepOutIV.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.width.equalTo(230)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        registerBtn.snp_makeConstraints { (make) in
            make.right.equalTo(messageLabel.snp_right)
//            make.top.equalTo(messageLabel.snp_bottom).offset(16)
//            make.size.equalTo(CGSize(width: 100, height: 35))
            
            make.top.equalTo(loginBtn)
            make.size.equalTo(loginBtn)
            
        }
        
    }
    
    
    
    
    //MARK : - 登陆按钮的监听事件
    @objc private func btnClick () {
        
        //执行闭包
        block?()
        
        
    }
    
    
    
    
    
    //MARK: - 懒加载方法创建控件
    //圆圈视图
    private lazy var feedIV : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //icon
    private lazy var iconIV : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //遮挡视图
    private lazy var keepOutIV : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //文字视图
    private lazy var messageLabel : UILabel = {
        let label = UILabel ()
        
        //设置文字内容
        label.text = "大家好大家好大家好大家好大家好大家好大家好大家好大家好大家好大家好"
        //设置文字大小
        label.font = UIFont.boldSystemFont(ofSize: 14)
        //设置文字颜色
        label.textColor = UIColor.darkGray
        //设置换行
        label.numberOfLines = 0
        //设置对齐方式
        label.textAlignment = .center
        
        return label
        
    } ()
    //登陆按钮
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton ()
        
        //设置按钮的背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        //设置按钮的文字
        btn.setTitle("登陆", for: UIControlState.normal)
        //设置文字的大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        //设置文字的颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        
        
        //给按钮添加监听事件
        btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
        
        
        
        
        return btn
    } ()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
        
        let btn1 = UIButton ()
        
        //设置按钮的背景图片
        btn1.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        //设置按钮的文字
        btn1.setTitle("注册", for: UIControlState.normal)
        //设置文字的大小
        btn1.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        //设置文字的颜色
        btn1.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn1.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        
        
        
        //给按钮添加监听事件
        btn1.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)
        
        
        
        
        return btn1
    } ()

    //共外界设置UI内容的方法
    func setUpSTFBaseViewInfo (imaName : String?,title : String?) {
        //其他3各界面
        if let img = imaName,let tit = title {
            //重写赋值
            iconIV.image = UIImage(named: img)
            messageLabel.text = tit
            
            //隐藏圆圈
            feedIV.isHidden = true
            
        } else {
            //主页面
            
            //执行动画
            feedIVAnim()
        }
        
    }
    
    //MARK: - 设置圆圈动画
    func feedIVAnim () {
        
        //核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
       
        anim.toValue = M_PI * 2
        
        anim.duration = 28
        //重复次数
        anim.repeatCount = MAXFLOAT
        //解决动画停止
        anim.isRemovedOnCompletion = false
        
        feedIV.layer.add(anim, forKey: nil)
        
    }
    
    
    
    
    
    
    
    
    
    
    
}

















